package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.EmployeeDAO;
import cn.edu.scau.cmi.hombio.dao.PigstyDAO;
import cn.edu.scau.cmi.hombio.dao.PreventionDAO;
import cn.edu.scau.cmi.hombio.dao.TaskDAO;

import cn.edu.scau.cmi.hombio.domain.Employee;
import cn.edu.scau.cmi.hombio.domain.Pigsty;
import cn.edu.scau.cmi.hombio.domain.Prevention;
import cn.edu.scau.cmi.hombio.domain.Task;

import cn.edu.scau.cmi.hombio.service.PreventionService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for Prevention entities
 * 
 */

@Controller("PreventionRestController")

public class PreventionRestController {

	/**
	 * DAO injected by Spring that manages Employee entities
	 * 
	 */
	@Autowired
	private EmployeeDAO employeeDAO;

	/**
	 * DAO injected by Spring that manages Pigsty entities
	 * 
	 */
	@Autowired
	private PigstyDAO pigstyDAO;

	/**
	 * DAO injected by Spring that manages Prevention entities
	 * 
	 */
	@Autowired
	private PreventionDAO preventionDAO;

	/**
	 * DAO injected by Spring that manages Task entities
	 * 
	 */
	@Autowired
	private TaskDAO taskDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for Prevention entities
	 * 
	 */
	@Autowired
	private PreventionService preventionService;

	/**
	 * View an existing Pigsty entity
	 * 
	 */
	@RequestMapping(value = "/Prevention/{prevention_id}/pigsty/{pigsty_id}", method = RequestMethod.GET)
	@ResponseBody
	public Pigsty loadPreventionPigsty(@PathVariable Integer prevention_id, @PathVariable Integer related_pigsty_id) {
		Pigsty pigsty = pigstyDAO.findPigstyByPrimaryKey(related_pigsty_id, -1, -1);

		return pigsty;
	}

	/**
	* Select an existing Prevention entity
	* 
	*/
	@RequestMapping(value = "/Prevention/{prevention_id}", method = RequestMethod.GET)
	@ResponseBody
	public Prevention loadPrevention(@PathVariable Integer prevention_id) {
		return preventionDAO.findPreventionByPrimaryKey(prevention_id);
	}

	/**
	* Delete an existing Prevention entity
	* 
	*/
	@RequestMapping(value = "/Prevention/{prevention_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deletePrevention(@PathVariable Integer prevention_id) {
		Prevention prevention = preventionDAO.findPreventionByPrimaryKey(prevention_id);
		preventionService.deletePrevention(prevention);
	}

	/**
	* Get Task entity by Prevention
	* 
	*/
	@RequestMapping(value = "/Prevention/{prevention_id}/task", method = RequestMethod.GET)
	@ResponseBody
	public Task getPreventionTask(@PathVariable Integer prevention_id) {
		return preventionDAO.findPreventionByPrimaryKey(prevention_id).getTask();
	}

	/**
	* Get Pigsty entity by Prevention
	* 
	*/
	@RequestMapping(value = "/Prevention/{prevention_id}/pigsty", method = RequestMethod.GET)
	@ResponseBody
	public Pigsty getPreventionPigsty(@PathVariable Integer prevention_id) {
		return preventionDAO.findPreventionByPrimaryKey(prevention_id).getPigsty();
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* Show all Prevention entities
	* 
	*/
	@RequestMapping(value = "/Prevention", method = RequestMethod.GET)
	@ResponseBody
	public List<Prevention> listPreventions() {
		return new java.util.ArrayList<Prevention>(preventionService.loadPreventions());
	}

	/**
	* View an existing Employee entity
	* 
	*/
	@RequestMapping(value = "/Prevention/{prevention_id}/employee/{employee_id}", method = RequestMethod.GET)
	@ResponseBody
	public Employee loadPreventionEmployee(@PathVariable Integer prevention_id, @PathVariable Integer related_employee_id) {
		Employee employee = employeeDAO.findEmployeeByPrimaryKey(related_employee_id, -1, -1);

		return employee;
	}

	/**
	* Create a new Employee entity
	* 
	*/
	@RequestMapping(value = "/Prevention/{prevention_id}/employee", method = RequestMethod.POST)
	@ResponseBody
	public Employee newPreventionEmployee(@PathVariable Integer prevention_id, @RequestBody Employee employee) {
		preventionService.savePreventionEmployee(prevention_id, employee);
		return employeeDAO.findEmployeeByPrimaryKey(employee.getId());
	}

	/**
	* Create a new Prevention entity
	* 
	*/
	@RequestMapping(value = "/Prevention", method = RequestMethod.POST)
	@ResponseBody
	public Prevention newPrevention(@RequestBody Prevention prevention) {
		preventionService.savePrevention(prevention);
		return preventionDAO.findPreventionByPrimaryKey(prevention.getId());
	}

	/**
	* Save an existing Employee entity
	* 
	*/
	@RequestMapping(value = "/Prevention/{prevention_id}/employee", method = RequestMethod.PUT)
	@ResponseBody
	public Employee savePreventionEmployee(@PathVariable Integer prevention_id, @RequestBody Employee employee) {
		preventionService.savePreventionEmployee(prevention_id, employee);
		return employeeDAO.findEmployeeByPrimaryKey(employee.getId());
	}

	/**
	* Create a new Task entity
	* 
	*/
	@RequestMapping(value = "/Prevention/{prevention_id}/task", method = RequestMethod.POST)
	@ResponseBody
	public Task newPreventionTask(@PathVariable Integer prevention_id, @RequestBody Task task) {
		preventionService.savePreventionTask(prevention_id, task);
		return taskDAO.findTaskByPrimaryKey(task.getId());
	}

	/**
	* Save an existing Prevention entity
	* 
	*/
	@RequestMapping(value = "/Prevention", method = RequestMethod.PUT)
	@ResponseBody
	public Prevention savePrevention(@RequestBody Prevention prevention) {
		preventionService.savePrevention(prevention);
		return preventionDAO.findPreventionByPrimaryKey(prevention.getId());
	}

	/**
	* Delete an existing Pigsty entity
	* 
	*/
	@RequestMapping(value = "/Prevention/{prevention_id}/pigsty/{pigsty_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deletePreventionPigsty(@PathVariable Integer prevention_id, @PathVariable Integer related_pigsty_id) {
		preventionService.deletePreventionPigsty(prevention_id, related_pigsty_id);
	}

	/**
	* Delete an existing Task entity
	* 
	*/
	@RequestMapping(value = "/Prevention/{prevention_id}/task/{task_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deletePreventionTask(@PathVariable Integer prevention_id, @PathVariable Integer related_task_id) {
		preventionService.deletePreventionTask(prevention_id, related_task_id);
	}

	/**
	* View an existing Task entity
	* 
	*/
	@RequestMapping(value = "/Prevention/{prevention_id}/task/{task_id}", method = RequestMethod.GET)
	@ResponseBody
	public Task loadPreventionTask(@PathVariable Integer prevention_id, @PathVariable Integer related_task_id) {
		Task task = taskDAO.findTaskByPrimaryKey(related_task_id, -1, -1);

		return task;
	}

	/**
	* Create a new Pigsty entity
	* 
	*/
	@RequestMapping(value = "/Prevention/{prevention_id}/pigsty", method = RequestMethod.POST)
	@ResponseBody
	public Pigsty newPreventionPigsty(@PathVariable Integer prevention_id, @RequestBody Pigsty pigsty) {
		preventionService.savePreventionPigsty(prevention_id, pigsty);
		return pigstyDAO.findPigstyByPrimaryKey(pigsty.getId());
	}

	/**
	* Save an existing Pigsty entity
	* 
	*/
	@RequestMapping(value = "/Prevention/{prevention_id}/pigsty", method = RequestMethod.PUT)
	@ResponseBody
	public Pigsty savePreventionPigsty(@PathVariable Integer prevention_id, @RequestBody Pigsty pigsty) {
		preventionService.savePreventionPigsty(prevention_id, pigsty);
		return pigstyDAO.findPigstyByPrimaryKey(pigsty.getId());
	}

	/**
	* Save an existing Task entity
	* 
	*/
	@RequestMapping(value = "/Prevention/{prevention_id}/task", method = RequestMethod.PUT)
	@ResponseBody
	public Task savePreventionTask(@PathVariable Integer prevention_id, @RequestBody Task task) {
		preventionService.savePreventionTask(prevention_id, task);
		return taskDAO.findTaskByPrimaryKey(task.getId());
	}

	/**
	* Get Employee entity by Prevention
	* 
	*/
	@RequestMapping(value = "/Prevention/{prevention_id}/employee", method = RequestMethod.GET)
	@ResponseBody
	public Employee getPreventionEmployee(@PathVariable Integer prevention_id) {
		return preventionDAO.findPreventionByPrimaryKey(prevention_id).getEmployee();
	}

	/**
	* Delete an existing Employee entity
	* 
	*/
	@RequestMapping(value = "/Prevention/{prevention_id}/employee/{employee_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deletePreventionEmployee(@PathVariable Integer prevention_id, @PathVariable Integer related_employee_id) {
		preventionService.deletePreventionEmployee(prevention_id, related_employee_id);
	}
}